// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <string>
#include <utility>

int const maxn = 200;
int f[maxn][maxn];
std::string g[maxn][maxn];
std::string s;

bool match(char ch1, char ch2)
{
	return (ch1 == '(' && ch2 == ')') || (ch1 == '[' && ch2 == ']');
}

std::string other(char ch, bool right)
{
	if (right) {
		if (ch == '(' || ch == ')') return ")";
		else return "]";
	} else {
		if (ch == '(' || ch == ')') return "(";
		else return "[";
	}
}

typedef std::pair<int, std::string> pair_type;
pair_type dfs(int l, int r)
{
	if (l > r) return std::make_pair(0, "");
	if (l == r)
		return std::make_pair(0, other(s[l - 1], 0) + other(s[l - 1], 1));
	if (f[l][r] != -1) return std::make_pair(f[l][r], g[l][r]);
	int ret = 0;
	std::string tmp;
	if (match(s[l - 1], s[r - 1])) {
		pair_type ts = dfs(l + 1, r - 1);
		ret = ts.first + 2;
		tmp = other(s[l - 1], 0) + ts.second + other(s[r - 1], 1);
	}
	for (int i = l + 1; i < r - 1; i++) {
		pair_type tl = dfs(l, i);
		pair_type tr = dfs(i + 1, r);
		if (tl.first + tr.first >= ret) {
			ret = tl.first + tr.first;
			tmp = tl.second + tr.second;
		}
	}
	pair_type t1 = dfs(l + 1, r);
	if (t1.first >= ret) {
		ret = t1.first;
		tmp = other(s[l - 1], 0) + t1.second + other(s[l - 1], 1);
	}
	pair_type t2 = dfs(l, r - 1);
	if (t2.first >= ret) {
		ret = t2.first;
		tmp = other(s[r - 1], 0) + t2.second + other(s[r - 1], 1);
	}
	f[l][r] = ret; g[l][r] = tmp;
	return std::make_pair(f[l][r], g[l][r]);
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	std::getline(std::cin, s);
	for (int ti = 1; ti <= T; ti++) {
		if (ti > 1) std::cout << '\n';
		std::getline(std::cin, s);
		std::getline(std::cin, s);
		int n = s.size();
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++) f[i][j] = -1;
		std::cout << dfs(1, n).second << '\n';
	}
}

